package org.chen.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.bao.utils.MyUtils;
import org.chen.bean.UserReceiptAddressBean;
import org.chen.service.impl.RecAddressServiceImpl;

/**
 * 添加收获地址
 */
@WebServlet("/RecMsgSaveAction")
public class RecMsgSaveAction extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * 保存收货地址
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获取表单数据
		UserReceiptAddressBean recB = MyUtils.convertFormData2Bean(request, UserReceiptAddressBean.class);
		
		//获取用户tel
		String usertel = (String) request.getSession().getAttribute("userTel");
		//将用户tel写入对象中
		recB.setUsertel(usertel);
		
		System.out.println("recB:" + recB.toString());
		// 将信息写入数据库中
		int i = MyUtils.getInstance(RecAddressServiceImpl.class).addRecAddress(recB);
		System.out.println("i:" + i);
		String istr = String.valueOf(i);

		// 添加后的跳转
		if (i > 0) {
			// 写入session
			request.getSession().setAttribute("recName", recB.getRecname());
			request.getSession().setAttribute("recTel", recB.getRectel());
			request.getSession().setAttribute("postCode", recB.getPostcode());
			request.getSession().setAttribute("recAddress", recB.getRecaddress());
			request.getSession().setAttribute("addressDetail", recB.getAddressdetail());
			
			PrintWriter out=response.getWriter();
			out.print("<script language='javascript'>alert('添加成功！');"
					+ "var index = parent.layer.getFrameIndex(window.name);parent.layer.close(index);</script>");
			
		} else {
			// 保存失败
			response.setContentType("text/html;charset=utf-8");
			PrintWriter out=response.getWriter();
			out.print("<script language='javascript'>alert('添加失败，请重新输入！');window.location.href='cwx/addinfo.jsp';</script>");
			
		}

	}

}
